package cal.lib.tweet;

import java.util.List;

import android.content.Context;
import android.content.Intent;
import android.content.pm.ApplicationInfo;
import android.net.Uri;

/**
 * This classes uses the folowing permissions :
 * 	<uses-permission android:name="android.permission.INTERNET"></uses-permission>
	<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"></uses-permission>
 * @author Chemical
 *
 */
public class TwittIt {

	
	private Context _ctx;
	private String _ErrorMessageIfNotFound;
	private Intent _theIntent;
	private String _msgToTweet=null;
	private Uri _bmToTweet=null;
	
	
	private String[] TwitterAppLis={
			"twit",
			"plume",
			"tweet",
			"hootsuite",
			"seesmic",
			"swift",
			"touiteur",
			"twicca",
			"twidroid"
	};
	
	/**
	 * Constant used to say to the Twitter class that we'll use the default app list
	 */
	public static final String[] NO_TWITTER_APP_LIST_PROVIDED=null;
	
	/**
	 * Create a Tweet from your message
	 * @param ctx the application context
	 * @param twitAppsList The list of Twitter apps you want to support (a basic list is provided : use TwittIt.NO_TWITTER_APP_LIST_PROVIDED . It includes
	 * Twitter, Plume, Tweet***, HootSuit, Swift, Touiteur, Twicca, Twidroid)
	 * @param ErrorMessageIfNotFound if it doesn't find any twitter apps it'll display the list of the whole activity that can send message and this errormessage will be the title
	 */
    public TwittIt(Context ctx, String[] twitAppsList, String ErrorMessageIfNotFound) {
        _ctx=ctx;
        _ErrorMessageIfNotFound=ErrorMessageIfNotFound;
		if(twitAppsList==null)twitAppsList=TwitterAppLis;
        
		_theIntent=getIntent(twitAppsList);
    }
	
	/**
	 * Create a Tweet from your message
	 * @param ctx the application context
	 * @param twitAppsList The list of Twitter apps you want to support (a basic list is provided : use TwittIt.NO_TWITTER_APP_LIST_PROVIDED . It includes
	 * Twitter, Plume, Tweet***, HootSuit, Swift, Touiteur, Twicca, Twidroid)
	 * @param errMsgResId the resource id of the 'error message'. If it doesn't find any twitter apps it'll display the list of the whole activity that can send message and this errormessage will be the title
	 */
    public TwittIt(Context ctx, String[] twitAppsList, int errMsgResId) {
        _ctx=ctx;
        _ErrorMessageIfNotFound= _ctx.getResources().getString(errMsgResId);
		if(twitAppsList==null)twitAppsList=TwitterAppLis;
        
		_theIntent=getIntent(twitAppsList);
    }
    
    /**
     * Methode to add a message to the tweet
     * @param message the message to add
     */
    public void addMessageToTweet(String message){
    	_msgToTweet=message;
    }
    
    /**
     * Methode to add a message to the tweet
     * @param resId the resource id of the message to add
     */
    public void addMessageToTweet(int resId){
    	_msgToTweet=_ctx.getResources().getString(resId);
    }

    /**
     * Methode to add a picture to the tweet
     * @param picPath the path of the picture to tweet
     */
    public void addPictureToTweet(String picPath){
    	_bmToTweet = Uri.parse(picPath);
    }

    /**
     * Methode to add a picture to the tweet
     * @param packageName the package name of the resource
     * @param resId the resource of the picture to add to the tweet
     */
    public void addPictureToTweet(int resId){
    	_bmToTweet = Uri.parse("android.resource://"+_ctx.getResources().getResourceName(resId).replace(":", "/"));
    }
    
    /**
     * Send the tweet you created
     */
    public void sendThisTweet(){
		_theIntent.setType("image/jpeg");
    	if(_msgToTweet!=null){
    		_theIntent.putExtra(Intent.EXTRA_TEXT, _msgToTweet);
    	}
    	if(_bmToTweet!=null){
    		_theIntent.putExtra(Intent.EXTRA_STREAM, _bmToTweet);
    	}
    	_ctx.startActivity(_theIntent);
    }
    
    
	private Intent getIntent(String[] intentIDS)
	{
		Intent intent = new Intent(Intent.ACTION_SEND);
		List<ApplicationInfo> appList = _ctx.getPackageManager().getInstalledApplications(0);

		boolean resolved =false;
		for(ApplicationInfo ri:appList)
		{
			for(int i=0;resolved==false && i<intentIDS.length && i<appList.size(); i++)
			{
				if(ri.packageName.toLowerCase().indexOf(intentIDS[i])>0)
				{
					intent.setPackage(ri.packageName);
					resolved = true;
				}
			}
		}
		if(!resolved) { intent=Intent.createChooser(intent, _ErrorMessageIfNotFound); }
		return intent;
	}
}